﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<link rel="stylesheet" type="text/css" media="screen" href="css/jquery-ui-1.8.1.custom.css" />
<link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" />
<script src="js/jquery-1.5.2.min.js" type="text/javascript"></script>
<script src="js/i18n/grid.locale-cn.js" type="text/javascript"></script>
<script src="js/json2.js" type="text/javascript"></script>
<script src="js/xing.js" type="text/javascript"></script>
<script type="text/javascript">
	$.jgrid.no_legacy_api = true;
	$.jgrid.useJSON = true;
</script>
<script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>
<script type="text/javascript">
var firstLoad = true;
var udistrict, usid;

function searchStreet() {
	var district = $("#sf_district").val();
	
	if (this.id == "sf_submit")
		jQuery("#list1").jqGrid('setGridParam',{page:1,url:"base?action=1&district=" + district}).trigger("reloadGrid");
	else
		$("#df").attr("src","base?oper=excel&action=1&district=" + district);
};

function setGridButton() {
	if ((!firstLoad) || (udistrict == "0") || (usid != "0"))
		return;
	
	jQuery("#list1").jqGrid('navGrid','#gridpager1',{edit:false,add:false,del:false,refresh:false,search:false})
		.jqGrid('navButtonAdd','#gridpager1',{
   		caption:"添加", 
   		buttonicon:"ui-icon-plus", 
   		onClickButton: function(){
				jQuery("#list1").jqGrid('editGridRow','new',{
					addCaption:"添加一个乡镇街道办事处",
					bSubmit:"保存",bCancel:"取消",modal: true, 
					closeAfterAdd:true, 
					reloadAfterSubmit:true,
					beforeShowForm:function(formid) {
						$('#sid').attr("readonly",false);
						$('#district').val(udistrict);
					},
					afterSubmit : function(r, postdata) {
			  					var data = eval('(' + r.responseText + ')');
			  					switch (data.code) {
			  						case 0: return [true, data.message]; break;
			  						case 999: return [false, "只能添加本县区乡镇街道社区信息"]; break;
			  						case 30000: return [false, "添加失败，编码不能重复！"]; break;
			  						default: return [false, data.message];
			  					}
			  	}
				}); 
			}
		})
		.jqGrid('navButtonAdd','#gridpager1',{
   		caption:"更改", 
   		buttonicon:"ui-icon-wrench", 
   		onClickButton: function(){
				var gr = jQuery("#list1").jqGrid('getGridParam','selrow');
				if ( gr == null ) {
					showTips("请选择要更改的乡镇街道办事处");
					return;
				}
				
				var district = jQuery("#list1").jqGrid('getCell', gr, 3);
				if (udistrict != district) {
					showTips("只能更改本县区的乡镇街道办事处");
					return;
				}
		  	jQuery("#list1").jqGrid('editGridRow',gr,{
		  		editCaption:"更改乡镇街道办事处信息",
		  		bSubmit:"保存",bCancel:"取消",modal: true,
		  		closeAfterEdit:true,
		  		viewPagerButtons:false, 
		  		reloadAfterSubmit:true,
		  		beforeShowForm:function(formid) {
		  			$('#sid').attr("readonly",true);
		  		},
					afterSubmit : function(r, postdata) {
		  					var data = eval('(' + r.responseText + ')');
		  					switch (data.code) {
		  						case 0: return [true, data.message]; break;
		  						case 999: return [false, "只能修改本区乡镇街道社区信息"]; break;
		  						default: return [false, data.message];
		  					}
					}
		  	});
			}
		})
		.jqGrid('navButtonAdd','#gridpager1',{
   		caption:"删除", 
   		buttonicon:"ui-icon-close", 
   		onClickButton: function(){   			
				var gr = jQuery("#list1").jqGrid('getGridParam','selrow');
				if( gr == null ) {
					showTips("请选择要删除的乡镇街道办事处");
					return;
				}
				
				var district = jQuery("#list1").jqGrid('getCell', gr, 3);
				if (udistrict != district) {
					showTips("只能删除本县区的街道");
					return;
				}
		  	jQuery("#list1").jqGrid('delGridRow', gr, {
		  		caption:"确定要删除这个乡镇街道办事处吗?", 
		  		msg:"确定要删除这个乡镇街道办事处？",
		  		bSubmit:"删除",bCancel:"取消",modal: true,
		  		closeAfterEdit:true, 
		  		reloadAfterSubmit:true,
		  		afterSubmit : function(r, postdata) {
		  					var data = eval('(' + r.responseText + ')');
		  					switch (data.code) {
		  						case 0: return [true, data.message]; break;
		  						case 30000: return [false, "要删除乡镇街道办事处，必须先删除其下的社区！"]; break;
		  						default: return [false, data.message];
		  					}
					}
		  	});
			}
		});
	
	firstLoad = false;
};

function setGrid() {
	jQuery("#list1").jqGrid({ 
			url:'base?action=1',
			datatype: "json",
			hidegrid: "false",
			colNames:['乡镇街道编码','乡镇街道名称','所属县区','地址','电话','邮政编码'], 
			colModel:[
					{name:'sid', width:80,align:'right',sortable:false,editable:true,editoptions:{size:8,maxlength:5},editrules:{required:true,number:true}}, 
					{name:'sname', width:180,align:'right', sortable:false,editable:true,editoptions:{size:18,maxlength:10},editrules:{required:true}},
					{name:'district',width:160,align:'right', sortable:false, editable:true,edittype:"select", formatter:'select', editoptions:{value:"1:市辖区;2:桥东区;3:桥西区;4:高新区;5:宣化区;6:下花园区"}},
					{name:'address', width:320,align:'right', sortable:false,editable: true, edittype:"textarea",editoptions:{rows:"2",cols:"18"},editrules:{required:true}},
					{name:'tel',width:180,align:'right', sortable:false,editable:true,editoptions:{size:16,maxlength:11},editrules:{required:true,number:true}},
					{name:'postcode', width:170,align:'right', sortable:false,editable:true,editoptions:{size:8,maxlength:6},editrules:{required:true,number:true}}
			], 
			viewrecords: true,
			rowNum:20,
			subGrid: true,
			shrinkToFit: false,
			pager: '#gridpager1',
			editurl:'base?action=1',
			caption:"乡镇街道办事处管理",
			height: 460,
			gridComplete: function() {
				udistrict = jQuery("#list1").jqGrid('getGridParam', 'userData')["udistrict"];
				usid = jQuery("#list1").jqGrid('getGridParam', 'userData')["usid"];
				setGridButton();
			},
			subGridRowExpanded: function(subgrid_id, row_id) {
				var subgrid_table_id, pager_id;
				subgrid_table_id = subgrid_id+"_t";
				subgridid = subgrid_table_id;
				pager_id = "p_"+subgrid_table_id;
				
				$("#"+subgrid_id).html("<table id='"+subgrid_table_id+"' class='scroll'></table><div id='"+pager_id+"' class='scroll'></div>");
				jQuery("#"+subgrid_table_id).jqGrid({
					url:"base?action=3&sid="+row_id,
					datatype: "json",
					colNames:['村社区编码','村社区名称','电话','办公地址'], 
					colModel:[
							{name:'cid', width:120,align:'right',sortable:false}, 
							{name:'cname', width:200,align:'right', sortable:false},
							{name:'tel',width:170,align:'right', sortable:false},
							{name:'address', width:290,align:'right', sortable:false}
					], 
					viewrecords: true,
					rowNum: 10,
					pager: pager_id, 
					height: '100%'
				});
			},
			subGridRowColapsed: function(subgrid_id, row_id) {
				// this function is called before removing the data
				var subgrid_table_id;
				subgrid_table_id = subgrid_id+"_t";
				jQuery("#"+subgrid_table_id).remove();
			}
  })
}; 
	
	$(document).ready(function(){
		setGrid();
		$("#sf_submit").click(searchStreet);
		$("#sf_excel").click(searchStreet);
	});
</script>
<body style="background-color:#d9f1cc;">
	<iframe id="df"  name="df" src="" width=0 height=0 frameborder="0" ></iframe>
	<table>
	<tr>
		<td>选择县区</td>
		<td><select id="sf_district" name="sf_district" onChange="sfDistrictChange()">
		                		<option value="0">全  市</option>
		                		<option value="1">市辖区</option>
		                		<option value="2">桥东区</option>
		                		<option value="3">桥西区</option>
		                		<option value="4">高新区</option>
		                		<option value="5">宣化区</option>
		                		<option value="6">下花园区</option>
		                	</select></td>
		</td>
		<td width="100" align="right"><input type="button" id="sf_submit" value="查 询"></td>
		<td><input type="button" id="sf_excel" value="导 出"></td>
	</tr>
	</table>
				<div id="RightPane">
					<div class="innertube">
					<table id="list1"></table>
					<div id="gridpager1"></div>
					</div>
				</div>

</body>
